package com.emmmya.ocs.common.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 作者：赖杰冠
 * 操作时间的工具类
 */
public class DateUtils {
    /**
     *
     * @param offset 偏移量 0获取当前月份，-1当前月上个月，1获取下个月
     *
     * */
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
    public static Integer getMonth(int offset){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH,offset);
        return cal.get(Calendar.MONTH)+1;
    }

    public static Date parseDate(String dateStr) throws ParseException {
        Date date = dateFormat.parse(dateStr);
        return date;
    }


    public static Date parseDate(String dateStr,String parseFormat) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(parseFormat);
        Date parse = format.parse(dateStr);
        return parse;
    }

    public static String formatDate(Date date){
        String parse = simpleDate.format(date);
        return parse;
    }

    public static String formatDate(Date date,String parseFormat){
        SimpleDateFormat format = new SimpleDateFormat(parseFormat);
        String parse = format.format(date);
        return parse;
    }


    /**
     * 获取某年的某月份最后一天
     *
     * @param month 获取的那个月份
     * @return
     */
    public static Integer getLastDay(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year); // 指定年份
        cal.set(Calendar.MONDAY, month); // 指定月份的下一个月
        int lastDay = cal.getMinimum(Calendar.DAY_OF_MONTH);// 获取月份中的最小值，即第一天
        cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); // 下一个月的第一天减去1就是指定月份的最后一天
        return cal.get(Calendar.DAY_OF_MONTH);
    }



    /**
     * 获取上个月的第一天
     */
    public static String getLastMonthFirshDay(Date now) {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            cal.add(Calendar.MONDAY, -1);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            return simpleDate.format(cal.getTime());
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取传过来的年份月份中的月份最后一天
     * @return YearMonth yyyy-MM格式  yyyy-MM-dd
     */
    public static String getLastDayTime(String YearMonth) {
        try {
            Calendar cal = Calendar.getInstance();
            Date parse = simpleDate.parse(YearMonth);
            cal.setTime(parse);
            cal.add(Calendar.MONTH, 1);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            return simpleDate.format(cal.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取传过来的年份月份中的月份最后一天
     * @return YearMonth yyyy-MM格式  yyyy-MM-dd
     */
    public static String getLastDayTime(Date YearMonth) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(YearMonth);
            cal.add(Calendar.MONTH, 1);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            return simpleDate.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取当前年份的月份里的第一天
     * @return yyyy-mm-dd
     */
    public static String getFirshDayTime(Date date) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            //Date parse = simpleDate.parse(YearMonth);
            //cal.setTime(parse);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            return simpleDate.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return "传入参数格式不对，请保持是yyyy-MM-dd格式";
        }
    }

    public static String getFirshDayTime(String date) {
        try {
            Calendar cal = Calendar.getInstance();
            Date parse = simpleDate.parse(date);
            cal.setTime(parse);
            //Date parse = simpleDate.parse(YearMonth);
            //cal.setTime(parse);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            return simpleDate.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return "传入参数格式不对，请保持是yyyy-MM-dd格式";
        }
    }

    public static String getTodayDate() {
        return simpleDate.format(new Date());
    }

    /**
     * 获取昨天日期 格式 YYYY-MM-DD
     * @return
     */
    public static String getYesterdayDate(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE,-1);
        return simpleDate.format(cal.getTime());
    }

    /**
     * 获取指定日期偏移左右天数的日期
     * @param date 被偏移的时间字符串
     * @param dev 偏移的天数  负数是往前的日期，正数时往后的日期
     * @return 偏移后的日期
     * @throws ParseException
     */
    public static String getOtherDay(String date,Integer dev) throws ParseException {
        Date parse = simpleDate.parse(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse);
        cal.add(Calendar.DAY_OF_YEAR,dev);
        return simpleDate.format(cal.getTime());

    }
    public static String getOtherDay(Date date,Integer dev) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR,dev);
        return simpleDate.format(cal.getTime());

    }

    public static void main(String[] args) {
        try {
            String otherDay = getOtherDay("2021-02-28", -29);
            System.out.println(otherDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

